package com.dhcc.DRGsHosp.application.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.inject.Named;

import org.dayatang.domain.EntityRepository;
import org.dayatang.domain.InstanceFactory;
import org.dayatang.querychannel.JdbcTemplateObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.dhcc.DRGsHosp.application.DrgFeeMedinsApplication;
import com.dhcc.DRGsHosp.core.domain.DrgFeeMedinsDetail;
import com.dhcc.DRGsHosp.core.domain.IndexCal;

/**
 * @title		:DrgFeeMedinsApplicationImpl.java
 * @description	:
 * @author		:huangju
 * @date		:2018年12月06日
 */
@Named("DrgFeeMedinsApplication")
@Transactional
public class DrgFeeMedinsApplicationImpl implements DrgFeeMedinsApplication {
	
	@Autowired
	private JdbcTemplateObj jdbcTemplateObj;
	
	private EntityRepository repository;
	private EntityRepository getRepository() {
		if (repository == null) {
			repository = InstanceFactory.getInstance(EntityRepository.class, "repository");
		}
		return repository;
	}
	
	/**
	 * DrgFeeMedinsDetail实体类的保存
	 */
	@Override
	public void saveDrgFeeMedinsDetail(DrgFeeMedinsDetail entity)throws Exception{  
		entity.save();
	}
	/**
	 * 
	 *  @Description：科室医保指标统计
	 *  @Author：huangju
	 *  @see com.dhcc.DRGsHosp.application.DrgFeeMedinsApplication#getDepMedinsQuota(com.dhcc.DRGsHosp.core.domain.IndexCal)
	 */
	@Override
	public List<Map<String, Object>> getDepMedinsQuota(IndexCal indexCal) {
  		String dateFrom = indexCal.getDateFrom();//开始时间
  		String dateTo = indexCal.getDateTo();//结束时间
		String sqlStr="select t.out_depdesc,t.out_depcode ,count(t.pmainindex) totalCaseCNt,\r\n" + 
				"round(avg(case when t.out_date-t.in_date=0 then 1 else t.out_date-t.in_date end),2) avgDays,\r\n" + 
				"round(avg(t.total_cost),2) avgFee,sum(t.profit) yingKui, \r\n"+ 
				"count(distinct t.drg_code) drgsCnt from DRGs.drgfee_medins t \r\n" + 
				"where to_char(out_date,'yyyy-mm-dd') between "+"'"+dateFrom+"'"+" and "+"'"+dateTo+"' \n";
		if(indexCal.getPardepCode()!=null&&!"".equals(indexCal.getPardepCode())) {
			String deps=indexCal.getPardepCode()==null?"":indexCal.getPardepCode();
			if(deps.indexOf(",")!=-1) {
				deps="'"+deps+"'";
				deps=deps.replace(",", "','");
				sqlStr+=" and t.out_depcode in ("+deps+") ";
			}else {
				sqlStr+=" and t.out_depcode = '"+indexCal.getPardepCode()+"'";
			}
		}
		sqlStr+="  group by t.out_depdesc,t.out_depcode";
		return jdbcTemplateObj.queryForList(sqlStr);
	}
	/**
	 * 
	 *  @Description：出院病历数列表
	 *  @Author：qiaoyilong
	 *  @see com.dhcc.DRGsHosp.application.DrgFeeMedinsApplication#getOutCases(com.dhcc.DRGsHosp.core.domain.IndexCal)
	 */
	@Override
	public List<Map<String, Object>> getOutCases(IndexCal indexCal) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		if(indexCal.getPardepCode()!=null && !"".equals(indexCal.getPardepCode()) ) {
			String depCode=indexCal.getPardepCode();
	  		String dateFrom = indexCal.getDateFrom();//开始时间
	  		String dateTo = indexCal.getDateTo();//结束时间
			String sqlStr=" select t.PMAININDEX,t.DRG_CODE ,t.DRG_DESC,t.PNAME,t.MR_NO,t.OUT_DEPCODE,t.OUT_DEPDESC,  \n" + 
					" TO_CHAR(t.IN_DATE, 'YYYY-MM-DD') AS IN_DATE,TO_CHAR(t.OUT_DATE, 'YYYY-MM-DD') AS OUT_DATE,TO_CHAR(t.PAYMENT_DATE, 'YYYY-MM-DD') AS PAYMENT_DATE, \n" + 
					" t.TOTAL_COST,t.PAYMENT_FOUNDATION,t.PAYMENT_INDIVIDUAL,t.PROFIT,t.DRG_COST, \n" +
					" case when t.out_date-t.in_date=0 then 1 else t.out_date-t.in_date end IN_DAYS \n" + 
					" from DRGs.drgfee_medins t \n" + 
					" where to_char(out_date,'yyyy-mm-dd') between "+"'"+dateFrom+"'"+" and "+"'"+dateTo+"' \n"+
					" and t.OUT_DEPCODE = '"+depCode+"'  "+
					" order by t.MR_NO,t.PMAININDEX asc ";
			list = jdbcTemplateObj.queryForList(sqlStr);
		}
		return list;
	}	
	/**
	 *  @Description：获取科室医保下的病组指标
	 *  @Author：huangju
	 *  @see com.dhcc.DRGsHosp.application.DrgFeeMedinsApplication#getDepUnderDrgsMedinsQuota(com.dhcc.DRGsHosp.facade.dto.IndexCalDTO)
	 */
	public List<Map<String,Object>> getDepUnderDrgsMedinsQuota(IndexCal indexCal){
		String dateFrom=indexCal.getDateFrom();//开始时间
		String dateTo = indexCal.getDateTo();//结束时间
		String depCode=indexCal.getPardepCode();
		String sqlStr="select t.drg_code,t.drg_desc,count(t.pmainindex) totalCaseCNt, \r\n"+
		       "round(avg(case when t.out_date-t.in_date=0 then 1 else t.out_date-t.in_date end),2) avgDays,\r\n" + 
		       "round(avg(t.total_cost),2) avgFee,sum(t.profit) yingKui from DRGs.drgfee_medins t \r\n"+ 
		       "where to_char(out_date,'yyyy-mm-dd') between "+"'"+dateFrom+"'"+" and "+"'"+dateTo+"' \r\n"+
		       "and t.out_depcode='"+depCode+"'   group by t.drg_code,t.drg_desc";
		return jdbcTemplateObj.queryForList(sqlStr);
	}
	/**
	 * 
	 * @Description：获得出院病历的分组的最新日期
	 * @Author huangju
	 */

	@Override
	public String getLatestDate() {
		StringBuilder sql = new StringBuilder();
		sql.append(" select max(t.out_date) from DRGs.drgfee_medins t \n ");
		List<Object> resultList = getRepository().createSqlQuery(sql.toString()).list();
		if (resultList.size() > 0) {
			return resultList.get(0) != null ? resultList.get(0).toString().substring(0, 10) : null;
		}
		return null;
	}
}

